/*
 * 文件名：StsQaJobCoreServiceImpl3_4.java
 * 版权：Copyright by www.yunshows.com
 * 描述：
 * 修改人：Awoke
 * 修改时间：2020年2月18日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.sts.service.impl;


import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.exception.ServiceException;
import top.infopub.job.bpa.service.EffectSureService;
import top.infopub.job.bpa.service.OutcaeSureService;
import top.infopub.job.fia.domain.ProBudgetOfgroupDtlDomain;
import top.infopub.job.fia.service.*;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.mkt.service.PreProjectService;
import top.infopub.job.sts.service.*;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.fia.LandProfit;
import top.infopub.model.fia.ProBudgetOfgroup;
import top.infopub.model.fia.ProBudgetOfgroupDtl;
import top.infopub.model.fia.SuperProfit;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.sts.OrgAreaQa;
import top.infopub.model.sts.OrgProQa;
import top.infopub.model.sts.OrgSuperQa;
import top.infopub.model.sts.ProInvestPool;
import top.infopub.tdp.util.BizParaUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 经营质量分析报表 数据归集汇总核心service 为给job服务</br>
 * 3=投资额 4=管理净利润 
 * @author Awoke
 * @version 2020年2月15日
 * @see StsQaJobCoreService3_4
 * @since
 */
@Service
public class StsQaJobCoreServiceImpl3_4 implements StsQaJobCoreService3_4 {

    private static final Logger log = LoggerFactory.getLogger(StsQaJobCoreService3_4.class);

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OrgSuperQaService orgSuperQaService;

    @Autowired
    private OrgAreaQaService orgAreaQaService;

    @Autowired
    private OrgProQaService orgProQaService;

    @Autowired
    private PreProjectService preProjectService;

    @Autowired
    private IncomeBillService incomeBillService;

    @Autowired
    private PayBillService payBillService;

    @Autowired
    private ProInvestPoolService proInvestPoolService;

    @Autowired
    private OutcaeSureService outcaeSureService;

    @Autowired
    private ProProfitPoolService proProfitPoolService;

    @Autowired
    private EffectSureService effectSureService;

    @Autowired
    private FeeNoteService feeNoteService;

    @Autowired
    private LandProfitService landProfitService;

    @Autowired
    private SuperProfitService superProfitService;

    @Autowired
    private ProBudgetOfgroupService proBudgetOfgroupService;

    @Autowired
    private ProBudgetOfgroupDtlService proBudgetOfgroupDtlService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsQaOfInvestJob() {
        /*
         * 建投总部/区域公司的 投资额 预算值为包含的项目公司预算值合计
         */

        // 1. 新增- 项目公司经营质量 数据
        Map<String, Object> proBudgetWhere = Maps.newHashMap();
        proBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        proBudgetWhere.put("processStatus", "9");
        // 【项目指标预算fia_pro_budget_ofgroup】{投资额}
        List<ProBudgetOfgroup> proBudgetList = proBudgetOfgroupService.selectByMapWhere(proBudgetWhere);
        // 最外层的for循环 使用预算的org反查其实际完成 
        for (ProBudgetOfgroup var : proBudgetList) {
            OrgProQa proQaAdd = new OrgProQa();
            OrgInfo temp = new OrgInfo();
            temp.setProjectCode(var.getProCode());
            List<OrgInfo> orgs = orgInfoService.selectByEntityWhere(temp);
            // 如果公司项目没有绑定 则跳过
            if (orgs == null || orgs.isEmpty()) {
                log.info("项目：{} 与公司未绑定，统计时，skip~", var.getProCode());
                continue;
            }
            else {
                proQaAdd.setHrOrgAreaId(orgs.get(0).getParentId());
                proQaAdd.setHrOrgProId(orgs.get(0).getId());
                proQaAdd.setOrgProName(orgs.get(0).getCompName());
            }
            proQaAdd.setProCode(var.getProCode());
            proQaAdd.setProName(var.getProName());
            proQaAdd.setAimType(3);// 指标类型 3=投资额
            proQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            proQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            proQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月

            // 当月计划
            Map<String, Object> proBudgetDetailWhere = Maps.newHashMap();
            proBudgetDetailWhere.put("proBudgetId", var.getId());
            proBudgetDetailWhere.put("months", ParserUtil.getMonthOfPrevMonth());
            ProBudgetOfgroupDtl plan = proBudgetOfgroupDtlService.selectByMapWhere(
                proBudgetDetailWhere).get(0);
            proQaAdd.setCurMonthPlan(plan.getInvestFee());// 原始单位：元
            proQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(plan.getInvestFee()));
            proQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(plan.getInvestFee()));
            // 年度计划
            proQaAdd.setYearAllPlan(var.getInvestSum());// 原始单位：元
            proQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(var.getInvestSum()));
            proQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(var.getInvestSum()));

            // 当月实际完成
            // 投资额 =对外建安产值确权+政府代付费+项目前期费+融资业务付息+融资业务付费
            Map<String, Object> proQaWhere = Maps.newHashMap();
            proQaWhere.put("payDateStart", ParserUtil.prevMonth());
            proQaWhere.put("payDateEnd", ParserUtil.currentMonth());
            proQaWhere.put("proCode", proQaAdd.getProCode());
            BigDecimal proQaOfInvest = payBillService.selectProQaOfInvest(proQaWhere);
            // 对外建安产值确权
            Map<String, Object> outSureWhere = Maps.newHashMap();
            outSureWhere.put("sureMonth", proQaAdd.getStsMonth());
            outSureWhere.put("proCode", proQaAdd.getProCode());
            BigDecimal proQaOfOutSure = outcaeSureService.selectProQaOfOutSure(outSureWhere);
            BigDecimal addUpSum = addUp(proQaOfInvest, proQaOfOutSure);
            // 实际当月完成 
            proQaAdd.setCurMonthFinish(addUpSum);
            proQaAdd.setCurMonthFinishTts(ParserUtil.unit2Tts(addUpSum));
            proQaAdd.setCurMonthFinishHms(ParserUtil.unit2Hms(addUpSum));
            if (proQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
                proQaAdd.setCurMonthRate(BigDecimal.ZERO);
            }
            else {
                proQaAdd.setCurMonthRate(proQaAdd.getCurMonthFinish().divide(
                    proQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }

            Map<String, Object> prevWhere = Maps.newHashMap();
            prevWhere.put("hrOrgProId", proQaAdd.getHrOrgProId());
            prevWhere.put("aimType", 3);// 投资额
            prevWhere.put("belongsYear", proQaAdd.getBelongsYear());
            prevWhere.put("belongsMonth", proQaAdd.getBelongsMonth() - 1);// 前一个月
            prevWhere.put("orderCondition", " add_time desc ");
            List<OrgProQa> prevList = orgProQaService.selectByMapWhere(prevWhere);

            // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据 
            if (proQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
                proQaAdd.setYearAddupPlan(proQaAdd.getCurMonthPlan());
                proQaAdd.setYearAddupPlanTts(proQaAdd.getCurMonthPlanTts());
                proQaAdd.setYearAddupPlanHms(proQaAdd.getCurMonthPlanHms());
                proQaAdd.setYearAddupFinish(proQaAdd.getCurMonthFinish());
                proQaAdd.setYearAddupFinishTts(proQaAdd.getCurMonthFinishTts());
                proQaAdd.setYearAddupFinishHms(proQaAdd.getCurMonthFinishHms());
                proQaAdd.setYearAddupRate(proQaAdd.getCurMonthRate());
            }
            else {
                OrgProQa prevProQa = prevList.get(0);
                // 累加   当前月累 = 上月月累 + 当前
                proQaAdd.setYearAddupPlan(proQaAdd.getCurMonthPlan().add(
                    prevProQa.getYearAddupPlan()));
                proQaAdd.setYearAddupPlanTts(proQaAdd.getCurMonthPlanTts().add(
                    prevProQa.getYearAddupPlanTts()));
                proQaAdd.setYearAddupPlanHms(proQaAdd.getCurMonthPlanHms().add(
                    prevProQa.getYearAddupPlanHms()));
                proQaAdd.setYearAddupFinish(proQaAdd.getCurMonthFinish().add(
                    prevProQa.getYearAddupFinish()));
                proQaAdd.setYearAddupFinishTts(proQaAdd.getCurMonthFinishTts().add(
                    prevProQa.getYearAddupFinishTts()));
                proQaAdd.setYearAddupFinishHms(proQaAdd.getCurMonthFinishHms().add(
                    prevProQa.getYearAddupFinishHms()));
                // 计算月累完成率
                if (proQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
                    proQaAdd.setYearAddupRate(BigDecimal.ZERO);
                }
                else {
                    proQaAdd.setYearAddupRate(proQaAdd.getYearAddupFinish().divide(
                        proQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                        ParserUtil.HUNDRED));// 完成率 =实际完成/计划
                }
            }
            // 年度完成率  月累完成/年度计划 
            if (proQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
                proQaAdd.setYearAllRate(BigDecimal.ZERO);
            }
            else {
                proQaAdd.setYearAllRate(proQaAdd.getYearAddupFinish().divide(
                    proQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));
            }
            proQaAdd.setAddBy("system");
            proQaAdd.setAddTime(new Date());
            proQaAdd.setUptBy("system");
            proQaAdd.setUptTime(new Date());
            orgProQaService.insert(proQaAdd);

            // 以下 新增 项目投资额 sts_pro_invest_pool数据
            ProInvestPool pool = new ProInvestPool();
            pool.setProCode(proQaAdd.getProCode());
            pool.setProName(proQaAdd.getProName());
            pool.setBelongsYear(proQaAdd.getBelongsYear());
            pool.setBelongsMonth(proQaAdd.getBelongsMonth());
            pool.setStsMonth(proQaAdd.getStsMonth());
            pool.setTotalSum(proQaAdd.getCurMonthFinish());
            pool.setTotalSumTts(proQaAdd.getCurMonthFinishTts());
            // 对外建安产值确权
            pool.setOutCaeSure(proQaOfOutSure);
            pool.setOutCaeSureTts(ParserUtil.unit2Tts(proQaOfOutSure));

            // 25=政府代付费 26=项目前期费 融资业务付息(27=建设期利息) 融资业务付费(28=融资费用 29=融资担保费)
            Map<String, Object> proPoolWhere = Maps.newHashMap();
            proQaWhere.put("payDateStart", ParserUtil.prevMonth());
            proQaWhere.put("payDateEnd", ParserUtil.currentMonth());
            proQaWhere.put("proCode", proQaAdd.getProCode());
            // 25=政府代付费 singleType
            proPoolWhere.put("singleType", "25");
            BigDecimal single_25 = payBillService.selectProSingleOfInvest(proQaWhere);
            pool.setGdPayFee(single_25);
            pool.setGdPayFeeTts(ParserUtil.unit2Tts(single_25));
            // 26=项目前期费  singleType
            proPoolWhere.put("singleType", "26");
            BigDecimal single_26 = payBillService.selectProSingleOfInvest(proQaWhere);
            pool.setProPreFee(single_26);
            pool.setProPreFeeTts(ParserUtil.unit2Tts(single_26));
            // 融资业务付息(27=建设期利息)  singleType
            proPoolWhere.put("singleType", "27");
            BigDecimal single_27 = payBillService.selectProSingleOfInvest(proQaWhere);
            pool.setFinBizInterest(single_27);
            pool.setFinBizInterestTts(ParserUtil.unit2Tts(single_27));
            // 融资业务付费(28=融资费用 29=融资担保费)
            proPoolWhere.put("singleType", "28,29");
            BigDecimal single_28_29 = payBillService.selectProSingleOfInvest(proQaWhere);
            pool.setFinBizFee(single_28_29);
            pool.setFinBizFeeTts(ParserUtil.unit2Tts(single_28_29));

            pool.setAddBy("system");
            pool.setAddTime(new Date());
            pool.setUptBy("system");
            pool.setUptTime(new Date());
            proInvestPoolService.insert(pool);
        }
        // 2. 新增- 区域公司经营质量 数据
        Map<String, Object> areaBudgetWhere = Maps.newHashMap();
        areaBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        areaBudgetWhere.put("months", ParserUtil.getMonthOfPrevMonth());
        List<ProBudgetOfgroupDtlDomain> areaMonthBudgetList = proBudgetOfgroupDtlService.sumMonthInvestOfArea(areaBudgetWhere);
        for (ProBudgetOfgroupDtlDomain var : areaMonthBudgetList) {
            if (StringUtils.isBlank(var.getHrOrgAreaId())) {
                log.info("所属区域公司ID为空，统计时，skip~");
                continue;
            }
            // 由于南充挂到总部下面，按着区域分组会汇总到，但不能计入区域数据
            if (StringUtils.endsWithIgnoreCase(var.getHrOrgAreaId(),
                BizParaUtil.get("super_org_id"))) {
                continue;
            }
            OrgAreaQa areaQaAdd = new OrgAreaQa();
            areaQaAdd.setHrOrgSuperId(BizParaUtil.get("super_org_id"));// 总部公司ID
            areaQaAdd.setHrOrgAreaId(var.getHrOrgAreaId());
            areaQaAdd.setOrgAreaName(orgInfoService.selectByPrimaryKey(var.getHrOrgAreaId()).getCompName());
            areaQaAdd.setAimType(3);// 指标类型 3=投资额
            areaQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            areaQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            areaQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月

            // 月度计划
            areaQaAdd.setCurMonthPlan(var.getInvestFee());// 原始单位：元
            areaQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(var.getInvestFee()));
            areaQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(var.getInvestFee()));
            // 年度预算
            Map<String, Object> areaYearBudgetWhere = Maps.newHashMap();
            areaYearBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
            areaYearBudgetWhere.put("hrOrgAreaId", var.getHrOrgAreaId());
            BigDecimal areaYearBudget = proBudgetOfgroupService.sumYearInvestOfArea(areaYearBudgetWhere);
            // 年度计划
            areaQaAdd.setYearAllPlan(areaYearBudget);// 原始单位：元
            areaQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(areaYearBudget));
            areaQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(areaYearBudget));

            // 当月实际完成
            // 投资额 =对外建安产值确权+政府代付费+项目前期费+融资业务付息+融资业务付费
            Map<String, Object> proQaWhere = Maps.newHashMap();
            proQaWhere.put("payDateStart", ParserUtil.prevMonth());
            proQaWhere.put("payDateEnd", ParserUtil.currentMonth());
            proQaWhere.put("hrOrgAreaId", var.getHrOrgAreaId());
            BigDecimal areaQaOfInvest = payBillService.selectAreaQaOfInvest(proQaWhere);
            // 对外建安产值确权
            Map<String, Object> outSureWhere = Maps.newHashMap();
            outSureWhere.put("sureMonth", areaQaAdd.getStsMonth());
            outSureWhere.put("hrOrgAreaId", var.getHrOrgAreaId());
            BigDecimal proQaOfOutSure = outcaeSureService.selectAreaQaOfOutSure(outSureWhere);
            BigDecimal addUpSum = addUp(areaQaOfInvest, proQaOfOutSure);
            // 实际当月完成 
            areaQaAdd.setCurMonthFinish(addUpSum);
            areaQaAdd.setCurMonthFinishTts(ParserUtil.unit2Tts(addUpSum));
            areaQaAdd.setCurMonthFinishHms(ParserUtil.unit2Hms(addUpSum));
            if (areaQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
                areaQaAdd.setCurMonthRate(BigDecimal.ZERO);
            }
            else {
                areaQaAdd.setCurMonthRate(areaQaAdd.getCurMonthFinish().divide(
                    areaQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }

            Map<String, Object> prevWhere = Maps.newHashMap();
            prevWhere.put("hrOrgAreaId", areaQaAdd.getHrOrgAreaId());
            prevWhere.put("aimType", 3);// 指标类型 3=投资额
            prevWhere.put("belongsYear", areaQaAdd.getBelongsYear());
            prevWhere.put("belongsMonth", areaQaAdd.getBelongsMonth() - 1);// 前一个月
            prevWhere.put("orderCondition", " add_time desc ");
            List<OrgAreaQa> prevList = orgAreaQaService.selectByMapWhere(prevWhere);

            // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据 
            if (areaQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
                areaQaAdd.setYearAddupPlan(areaQaAdd.getCurMonthPlan());
                areaQaAdd.setYearAddupPlanTts(areaQaAdd.getCurMonthPlanTts());
                areaQaAdd.setYearAddupPlanHms(areaQaAdd.getCurMonthPlanHms());
                areaQaAdd.setYearAddupFinish(areaQaAdd.getCurMonthFinish());
                areaQaAdd.setYearAddupFinishTts(areaQaAdd.getCurMonthFinishTts());
                areaQaAdd.setYearAddupFinishHms(areaQaAdd.getCurMonthFinishHms());
                areaQaAdd.setYearAddupRate(areaQaAdd.getCurMonthRate());
            }
            else {
                OrgAreaQa prevAreaQa = prevList.get(0);
                // 累加   当前月累 = 上月月累 + 当前
                areaQaAdd.setYearAddupPlan(areaQaAdd.getCurMonthPlan().add(
                    prevAreaQa.getYearAddupPlan()));
                areaQaAdd.setYearAddupPlanTts(areaQaAdd.getCurMonthPlanTts().add(
                    prevAreaQa.getYearAddupPlanTts()));
                areaQaAdd.setYearAddupPlanHms(areaQaAdd.getCurMonthPlanHms().add(
                    prevAreaQa.getYearAddupPlanHms()));
                areaQaAdd.setYearAddupFinish(areaQaAdd.getCurMonthFinish().add(
                    prevAreaQa.getYearAddupFinish()));
                areaQaAdd.setYearAddupFinishTts(areaQaAdd.getCurMonthFinishTts().add(
                    prevAreaQa.getYearAddupFinishTts()));
                areaQaAdd.setYearAddupFinishHms(areaQaAdd.getCurMonthFinishHms().add(
                    prevAreaQa.getYearAddupFinishHms()));
                // 计算月累完成率
                if (areaQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
                    areaQaAdd.setYearAddupRate(BigDecimal.ZERO);
                }
                else {
                    areaQaAdd.setYearAddupRate(areaQaAdd.getYearAddupFinish().divide(
                        areaQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                        ParserUtil.HUNDRED));// 完成率 =实际完成/计划
                }
            }
            // 年度完成率  月累完成/年度计划 
            if (areaQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
                areaQaAdd.setYearAllRate(BigDecimal.ZERO);
            }
            else {
                areaQaAdd.setYearAllRate(areaQaAdd.getYearAddupFinish().divide(
                    areaQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));
            }
            areaQaAdd.setAddBy("system");
            areaQaAdd.setAddTime(new Date());
            areaQaAdd.setUptBy("system");
            areaQaAdd.setUptTime(new Date());
            orgAreaQaService.insert(areaQaAdd);
        }
        // 3. 新增- 总部公司经营质量 数据
        // 月度预算
        Map<String, Object> superBudgetWhere = Maps.newHashMap();
        superBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        superBudgetWhere.put("months", ParserUtil.getMonthOfPrevMonth());
        BigDecimal superMonthBudget = proBudgetOfgroupDtlService.sumMonthInvestOfSuper(superBudgetWhere);
        if (superMonthBudget == null || superMonthBudget.compareTo(BigDecimal.ZERO) == 0) {
            throw new ServiceException("[总部]经营质量分析指标-投资额 数据归集时，公司预算不存在~");
        }
        // 年度预算
        Map<String, Object> superYearBudgetWhere = Maps.newHashMap();
        superYearBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        BigDecimal superYearBudget = proBudgetOfgroupService.sumYearInvestOfSuper(superYearBudgetWhere);
        if (superYearBudget == null || superYearBudget.compareTo(BigDecimal.ZERO) == 0) {
            throw new ServiceException("[总部]经营质量分析指标-投资额 数据归集时，公司预算不存在~");
        }

        OrgSuperQa superQaAdd = new OrgSuperQa();
        superQaAdd.setHrOrgSuperId(BizParaUtil.get("super_org_id"));// 总部公司ID
        superQaAdd.setAimType(3);// 指标类型 3=投资额
        superQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
        superQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
        superQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月
        // 月度计划
        superQaAdd.setCurMonthPlan(superMonthBudget);// 原始单位：元
        superQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(superMonthBudget));
        superQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(superMonthBudget));
        // 年度计划
        superQaAdd.setYearAllPlan(superYearBudget);// 原始单位：元
        superQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(superYearBudget));
        superQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(superYearBudget));

        // 当月实际完成
        // 投资额 =对外建安产值确权+政府代付费+项目前期费+融资业务付息+融资业务付费
        Map<String, Object> proQaWhere = Maps.newHashMap();
        proQaWhere.put("payDateStart", ParserUtil.prevMonth());
        proQaWhere.put("payDateEnd", ParserUtil.currentMonth());
        BigDecimal superQaOfInvest = payBillService.selectAllProQaOfInvest(proQaWhere);
        // 对外建安产值确权
        Map<String, Object> outSureWhere = Maps.newHashMap();
        outSureWhere.put("sureMonth", superQaAdd.getStsMonth());
        BigDecimal proQaOfOutSure = outcaeSureService.selectAllProQaOfOutSure(outSureWhere);
        BigDecimal addUpSum = addUp(superQaOfInvest, proQaOfOutSure);
        // 实际当月完成 
        superQaAdd.setCurMonthFinish(addUpSum);
        superQaAdd.setCurMonthFinishTts(ParserUtil.unit2Tts(addUpSum));
        superQaAdd.setCurMonthFinishHms(ParserUtil.unit2Hms(addUpSum));
        if (superQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAdd.setCurMonthRate(BigDecimal.ZERO);
        }
        else {
            superQaAdd.setCurMonthRate(superQaAdd.getCurMonthFinish().divide(
                superQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));// 完成率 =实际完成/计划
        }

        Map<String, Object> prevWhere = Maps.newHashMap();
        prevWhere.put("hrOrgSuperId", BizParaUtil.get("super_org_id"));// 总部公司ID
        prevWhere.put("aimType", 3);// 指标类型 3=投资额
        prevWhere.put("belongsYear", superQaAdd.getBelongsYear());
        prevWhere.put("belongsMonth", superQaAdd.getBelongsMonth() - 1);// 前一个月
        prevWhere.put("orderCondition", " add_time desc ");
        List<OrgSuperQa> prevList = orgSuperQaService.selectByMapWhere(prevWhere);

        // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据 
        if (superQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan());
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts());
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms());
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish());
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts());
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms());
            superQaAdd.setYearAddupRate(superQaAdd.getCurMonthRate());
        }
        else {
            OrgSuperQa prevAreaQa = prevList.get(0);
            // 累加   当前月累 = 上月月累 + 当前
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan().add(
                prevAreaQa.getYearAddupPlan()));
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts().add(
                prevAreaQa.getYearAddupPlanTts()));
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms().add(
                prevAreaQa.getYearAddupPlanHms()));
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish().add(
                prevAreaQa.getYearAddupFinish()));
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts().add(
                prevAreaQa.getYearAddupFinishTts()));
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms().add(
                prevAreaQa.getYearAddupFinishHms()));
            // 计算月累完成率
            if (superQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
                superQaAdd.setYearAddupRate(BigDecimal.ZERO);
            }
            else {
                superQaAdd.setYearAddupRate(superQaAdd.getYearAddupFinish().divide(
                    superQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }
        }
        // 年度完成率  月累完成/年度计划 
        if (superQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAdd.setYearAllRate(BigDecimal.ZERO);
        }
        else {
            superQaAdd.setYearAllRate(superQaAdd.getYearAddupFinish().divide(
                superQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
        }
        superQaAdd.setAddBy("system");
        superQaAdd.setAddTime(new Date());
        superQaAdd.setUptBy("system");
        superQaAdd.setUptTime(new Date());
        orgSuperQaService.insert(superQaAdd);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsQaOfProfitJob() {
        /*
         * 建投总部/区域公司的管理净利润 预算值为包含的项目公司预算值合计
         */

        // 累计 总部当月实际完成
        BigDecimal sumSuperProfit = BigDecimal.ZERO;

        // 2. 新增- 区域公司经营质量 数据
        Map<String, Object> areaBudgetWhere = Maps.newHashMap();
        areaBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        areaBudgetWhere.put("months", ParserUtil.getMonthOfPrevMonth());
        List<ProBudgetOfgroupDtlDomain> areaMonthBudgetList = proBudgetOfgroupDtlService.sumMonthProfitOfArea(areaBudgetWhere);
        // map key=区域id value=区域公司 累计管理净利润
        Map<String, BigDecimal> areaProfitMap = Maps.newHashMap();
        for (ProBudgetOfgroupDtlDomain var : areaMonthBudgetList) {
            if (StringUtils.isBlank(var.getHrOrgAreaId())) {
                continue;
            }
            // 由于南充等项目 挂到总部下面，按着区域分组会汇总到，但不能计入区域数据
            if (StringUtils.endsWithIgnoreCase(var.getHrOrgAreaId(),
                BizParaUtil.get("super_org_id"))) {
                continue;
            }
            areaProfitMap.put(var.getHrOrgAreaId(), BigDecimal.ZERO);
        }

        // 1. 新增- 项目公司经营质量 数据
        Map<String, Object> proBudgetWhere = Maps.newHashMap();
        proBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        proBudgetWhere.put("processStatus", "9");
        // 【项目指标预算fia_pro_budget_ofgroup】{管理净利润}
        List<ProBudgetOfgroup> proBudgetList = proBudgetOfgroupService.selectByMapWhere(proBudgetWhere);
        // 最外层的for循环 使用预算的org反查其实际完成 
//        for (ProBudgetOfgroup var : proBudgetList) {
//            OrgProQa proQaAdd = new OrgProQa();
//            OrgInfo temp = new OrgInfo();
//            temp.setProjectCode(var.getProCode());
//            List<OrgInfo> orgs = orgInfoService.selectByEntityWhere(temp);
//            // 如果公司项目没有绑定 则跳过
//            if (orgs == null || orgs.isEmpty()) {
//                log.info("项目：{} 与公司未绑定，统计时，skip~", var.getProCode());
//                continue;
//            }
//            else {
//                // 注意：不归集南充项目的净利润
//                if (StringUtils.endsWithIgnoreCase(orgs.get(0).getId(),
//                    BizParaUtil.get("land_org_id"))) {
//                    continue;
//                }
//                proQaAdd.setHrOrgAreaId(orgs.get(0).getParentId());
//                proQaAdd.setHrOrgProId(orgs.get(0).getId());
//                proQaAdd.setOrgProName(orgs.get(0).getCompName());
//            }
//            proQaAdd.setProCode(var.getProCode());
//            proQaAdd.setProName(var.getProName());
//            proQaAdd.setAimType(4);// 指标类型 4=管理净利润
//            proQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
//            proQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
//            proQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月
//
//            // 当月计划
//            Map<String, Object> proBudgetDetailWhere = Maps.newHashMap();
//            proBudgetDetailWhere.put("proBudgetId", var.getId());
//            proBudgetDetailWhere.put("months", ParserUtil.getMonthOfPrevMonth());
//            ProBudgetOfgroupDtl plan = proBudgetOfgroupDtlService.selectByMapWhere(
//                proBudgetDetailWhere).get(0);
//            proQaAdd.setCurMonthPlan(plan.getManageProfit());// 原始单位：元
//            proQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(plan.getManageProfit()));
//            proQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(plan.getInvestFee()));
//            // 年度计划
//            proQaAdd.setYearAllPlan(var.getManageProfitSum());// 原始单位：元
//            proQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(var.getManageProfitSum()));
//            proQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(var.getManageProfitSum()));
//
//            // 当月实际完成
//            // 若项目归属公司为项目公司（非南充中南建设发展有限公司），则PPP项目管理净利润：
//            // 1. 建安毛利
//            Map<String, Object> grossProfitWhere = Maps.newHashMap();
//            grossProfitWhere.put("sureMonth", proQaAdd.getStsMonth());
//            grossProfitWhere.put("proCode", proQaAdd.getProCode());
//            BigDecimal grossProfit = outcaeSureService.selectProQaOfGrossProfit(grossProfitWhere);
//            // 2. 创效金额
//            Map<String, Object> effectFeeWhere = Maps.newHashMap();
//            effectFeeWhere.put("stsMonth", proQaAdd.getStsMonth());
//            effectFeeWhere.put("hrOrgProId", proQaAdd.getHrOrgProId());
//            // 创效金额计算方式修改为   （ 开源创效=20，30=节流降本 PROFIT_FLAG=1 ）
//            BigDecimal effectFee = effectSureService.selectTypeOfEffectFee(effectFeeWhere);
//            // 3. 融资息差
//            BigDecimal finSpread = feeNoteService.selectProQaOfFinSpread(ParserUtil.prevMonth(),
//                ParserUtil.currentMonth(), proQaAdd.getHrOrgProId(), proQaAdd.getProCode());
//            // 4. 管理费用
//            Map<String, Object> manageFeeWhere = Maps.newHashMap();
//            manageFeeWhere.put("stsMonth", proQaAdd.getStsMonth());
//            manageFeeWhere.put("hrOrgProId", proQaAdd.getHrOrgProId());
//            BigDecimal manageFee = feeNoteService.selectProQaOfManageFee(manageFeeWhere);
//            // 5. 财务费用
//            Map<String, Object> fiaFeeWhere = Maps.newHashMap();
//            fiaFeeWhere.put("stsMonth", proQaAdd.getStsMonth());
//            fiaFeeWhere.put("hrOrgProId", proQaAdd.getHrOrgProId());
//            BigDecimal fiaFee = feeNoteService.selectProQaOfFiaFee(fiaFeeWhere);
//            // 6. 税金
//            Map<String, Object> taxFeeWhere = Maps.newHashMap();
//            taxFeeWhere.put("payDateStart", ParserUtil.prevMonth());
//            taxFeeWhere.put("payDateEnd", ParserUtil.currentMonth());
//            taxFeeWhere.put("proCode", proQaAdd.getProCode());
//            BigDecimal taxFee = payBillService.selectProQaOfTaxFee(taxFeeWhere);
//            // 计算管理净利润
//            BigDecimal cpuProfit = cpuProfit(grossProfit, effectFee, finSpread, manageFee, fiaFee,
//                taxFee);
//            proQaAdd.setCurMonthFinish(cpuProfit);
//            proQaAdd.setCurMonthFinishTts(ParserUtil.unit2Tts(cpuProfit));
//            proQaAdd.setCurMonthFinishHms(ParserUtil.unit2Hms(cpuProfit));
//            if (proQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
//                proQaAdd.setCurMonthRate(BigDecimal.ZERO);
//            }
//            else {
//                proQaAdd.setCurMonthRate(proQaAdd.getCurMonthFinish().divide(
//                    proQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
//                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
//            }
//
//            // 累加到总部 当月实际完成
//            sumSuperProfit = sumSuperProfit.add(cpuProfit);
//
//            // 累加到所属区域 当月实际完成
//            if (areaProfitMap.containsKey(proQaAdd.getHrOrgAreaId())) {
//                areaProfitMap.put(proQaAdd.getHrOrgAreaId(),
//                    areaProfitMap.get(proQaAdd.getHrOrgAreaId()).add(cpuProfit));
//            }
//
//            Map<String, Object> prevWhere = Maps.newHashMap();
//            prevWhere.put("hrOrgProId", proQaAdd.getHrOrgProId());
//            prevWhere.put("aimType", 4);// 管理净利润
//            prevWhere.put("belongsYear", proQaAdd.getBelongsYear());
//            prevWhere.put("belongsMonth", proQaAdd.getBelongsMonth() - 1);// 前一个月
//            prevWhere.put("orderCondition", " add_time desc ");
//            List<OrgProQa> prevList = orgProQaService.selectByMapWhere(prevWhere);
//
//            // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据
//            if (proQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
//                proQaAdd.setYearAddupPlan(proQaAdd.getCurMonthPlan());
//                proQaAdd.setYearAddupPlanTts(proQaAdd.getCurMonthPlanTts());
//                proQaAdd.setYearAddupPlanHms(proQaAdd.getCurMonthPlanHms());
//                proQaAdd.setYearAddupFinish(proQaAdd.getCurMonthFinish());
//                proQaAdd.setYearAddupFinishTts(proQaAdd.getCurMonthFinishTts());
//                proQaAdd.setYearAddupFinishHms(proQaAdd.getCurMonthFinishHms());
//                proQaAdd.setYearAddupRate(proQaAdd.getCurMonthRate());
//            }
//            else {
//                OrgProQa prevProQa = prevList.get(0);
//                // 累加   当前月累 = 上月月累 + 当前
//                proQaAdd.setYearAddupPlan(proQaAdd.getCurMonthPlan().add(
//                    prevProQa.getYearAddupPlan()));
//                proQaAdd.setYearAddupPlanTts(proQaAdd.getCurMonthPlanTts().add(
//                    prevProQa.getYearAddupPlanTts()));
//                proQaAdd.setYearAddupPlanHms(proQaAdd.getCurMonthPlanHms().add(
//                    prevProQa.getYearAddupPlanHms()));
//                proQaAdd.setYearAddupFinish(proQaAdd.getCurMonthFinish().add(
//                    prevProQa.getYearAddupFinish()));
//                proQaAdd.setYearAddupFinishTts(proQaAdd.getCurMonthFinishTts().add(
//                    prevProQa.getYearAddupFinishTts()));
//                proQaAdd.setYearAddupFinishHms(proQaAdd.getCurMonthFinishHms().add(
//                    prevProQa.getYearAddupFinishHms()));
//                // 计算月累完成率
//                if (proQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
//                    proQaAdd.setYearAddupRate(BigDecimal.ZERO);
//                }
//                else {
//                    proQaAdd.setYearAddupRate(proQaAdd.getYearAddupFinish().divide(
//                        proQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
//                        ParserUtil.HUNDRED));// 完成率 =实际完成/计划
//                }
//            }
//            // 年度完成率  月累完成/年度计划
//            if (proQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
//                proQaAdd.setYearAllRate(BigDecimal.ZERO);
//            }
//            else {
//                proQaAdd.setYearAllRate(proQaAdd.getYearAddupFinish().divide(
//                    proQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
//                    ParserUtil.HUNDRED));
//            }
//            proQaAdd.setAddBy("system");
//            proQaAdd.setAddTime(new Date());
//            proQaAdd.setUptBy("system");
//            proQaAdd.setUptTime(new Date());
//            orgProQaService.insert(proQaAdd);
//
//            // 以下 新增  项目管理净利润（非南充）  sts_pro_profit_pool数据
//            ProProfitPool pool = new ProProfitPool();
//            pool.setProCode(proQaAdd.getProCode());
//            pool.setProName(proQaAdd.getProName());
//            pool.setBelongsYear(proQaAdd.getBelongsYear());
//            pool.setBelongsMonth(proQaAdd.getBelongsMonth());
//            pool.setStsMonth(proQaAdd.getStsMonth());
//            pool.setTotalSum(proQaAdd.getCurMonthFinish());
//            pool.setTotalSumTts(proQaAdd.getCurMonthFinishTts());
//            // 1. 建安毛利
//            pool.setCaeGrossProfit(grossProfit);
//            pool.setCaeGrossProfitTts(ParserUtil.unit2Tts(grossProfit));
//            // 2. 创效金额
//            pool.setEffectFee(effectFee);
//            pool.setEffectFeeTts(ParserUtil.unit2Tts(effectFee));
//            // 3. 融资息差
//            pool.setFinSpread(finSpread);
//            pool.setFinSpreadTts(ParserUtil.unit2Tts(finSpread));
//            // 4. 管理费用
//            pool.setManageFee(manageFee);
//            pool.setManageFeeTts(ParserUtil.unit2Tts(manageFee));
//            // 5. 财务费用
//            pool.setFiaFee(fiaFee);
//            pool.setFiaFeeTts(ParserUtil.unit2Tts(fiaFee));
//            // 6. 税金
//            pool.setTaxFee(taxFee);
//            pool.setTaxFeeTts(ParserUtil.unit2Tts(taxFee));
//
//            pool.setAddBy("system");
//            pool.setAddTime(new Date());
//            pool.setUptBy("system");
//            pool.setUptTime(new Date());
//            proProfitPoolService.insert(pool);
//        }
        // 2. 新增- 区域公司经营质量 数据
        for (ProBudgetOfgroupDtlDomain var : areaMonthBudgetList) {
            if (StringUtils.isBlank(var.getHrOrgAreaId())) {
                log.info("所属区域公司ID为空，统计时，skip~");
                continue;
            }
            // 由于南充挂到总部下面，按着区域分组会汇总到，但不能计入区域数据
            if (StringUtils.endsWithIgnoreCase(var.getHrOrgAreaId(),
                BizParaUtil.get("super_org_id"))) {
                continue;
            }
            OrgAreaQa areaQaAdd = new OrgAreaQa();
            areaQaAdd.setHrOrgSuperId(BizParaUtil.get("super_org_id"));// 总部公司ID
            areaQaAdd.setHrOrgAreaId(var.getHrOrgAreaId());
            areaQaAdd.setOrgAreaName(orgInfoService.selectByPrimaryKey(var.getHrOrgAreaId()).getCompName());
            areaQaAdd.setAimType(4);// 指标类型 4=管理净利润
            areaQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            areaQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            areaQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月

            // 月度计划
            areaQaAdd.setCurMonthPlan(var.getManageProfit());// 原始单位：元
            areaQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(var.getManageProfit()));
            areaQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(var.getManageProfit()));
            // 年度预算
            Map<String, Object> areaYearBudgetWhere = Maps.newHashMap();
            areaYearBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
            areaYearBudgetWhere.put("hrOrgAreaId", var.getHrOrgAreaId());
            BigDecimal areaYearBudget = proBudgetOfgroupService.sumYearProfitOfArea(areaYearBudgetWhere);
            // 年度计划
            areaQaAdd.setYearAllPlan(areaYearBudget);// 原始单位：元
            areaQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(areaYearBudget));
            areaQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(areaYearBudget));

            // 实际当月完成 
            // 取值 前面累加计算而来的值 
            BigDecimal areaProfit = areaProfitMap.get(var.getHrOrgAreaId());
            areaQaAdd.setCurMonthFinish(areaProfit);
            areaQaAdd.setCurMonthFinishTts(ParserUtil.unit2Tts(areaProfit));
            areaQaAdd.setCurMonthFinishHms(ParserUtil.unit2Hms(areaProfit));
            if (areaQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
                areaQaAdd.setCurMonthRate(BigDecimal.ZERO);
            }
            else {
                areaQaAdd.setCurMonthRate(areaQaAdd.getCurMonthFinish().divide(
                    areaQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }

            Map<String, Object> prevWhere = Maps.newHashMap();
            prevWhere.put("hrOrgAreaId", areaQaAdd.getHrOrgAreaId());
            prevWhere.put("aimType", 4);// 指标类型 4=管理净利润
            prevWhere.put("belongsYear", areaQaAdd.getBelongsYear());
            prevWhere.put("belongsMonth", areaQaAdd.getBelongsMonth() - 1);// 前一个月
            prevWhere.put("orderCondition", " add_time desc ");
            List<OrgAreaQa> prevList = orgAreaQaService.selectByMapWhere(prevWhere);

            // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据 
            if (areaQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
                areaQaAdd.setYearAddupPlan(areaQaAdd.getCurMonthPlan());
                areaQaAdd.setYearAddupPlanTts(areaQaAdd.getCurMonthPlanTts());
                areaQaAdd.setYearAddupPlanHms(areaQaAdd.getCurMonthPlanHms());
                areaQaAdd.setYearAddupFinish(areaQaAdd.getCurMonthFinish());
                areaQaAdd.setYearAddupFinishTts(areaQaAdd.getCurMonthFinishTts());
                areaQaAdd.setYearAddupFinishHms(areaQaAdd.getCurMonthFinishHms());
                areaQaAdd.setYearAddupRate(areaQaAdd.getCurMonthRate());
            }
            else {
                OrgAreaQa prevAreaQa = prevList.get(0);
                // 累加   当前月累 = 上月月累 + 当前
                areaQaAdd.setYearAddupPlan(areaQaAdd.getCurMonthPlan().add(
                    prevAreaQa.getYearAddupPlan()));
                areaQaAdd.setYearAddupPlanTts(areaQaAdd.getCurMonthPlanTts().add(
                    prevAreaQa.getYearAddupPlanTts()));
                areaQaAdd.setYearAddupPlanHms(areaQaAdd.getCurMonthPlanHms().add(
                    prevAreaQa.getYearAddupPlanHms()));
                areaQaAdd.setYearAddupFinish(areaQaAdd.getCurMonthFinish().add(
                    prevAreaQa.getYearAddupFinish()));
                areaQaAdd.setYearAddupFinishTts(areaQaAdd.getCurMonthFinishTts().add(
                    prevAreaQa.getYearAddupFinishTts()));
                areaQaAdd.setYearAddupFinishHms(areaQaAdd.getCurMonthFinishHms().add(
                    prevAreaQa.getYearAddupFinishHms()));
                // 计算月累完成率
                if (areaQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
                    areaQaAdd.setYearAddupRate(BigDecimal.ZERO);
                }
                else {
                    areaQaAdd.setYearAddupRate(areaQaAdd.getYearAddupFinish().divide(
                        areaQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                        ParserUtil.HUNDRED));// 完成率 =实际完成/计划
                }
            }
            // 年度完成率  月累完成/年度计划 
            if (areaQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
                areaQaAdd.setYearAllRate(BigDecimal.ZERO);
            }
            else {
                areaQaAdd.setYearAllRate(areaQaAdd.getYearAddupFinish().divide(
                    areaQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));
            }
            areaQaAdd.setAddBy("system");
            areaQaAdd.setAddTime(new Date());
            areaQaAdd.setUptBy("system");
            areaQaAdd.setUptTime(new Date());
            orgAreaQaService.insert(areaQaAdd);
        }
        // 3. 新增- 总部公司经营质量 数据
        // 月度预算
        Map<String, Object> superBudgetWhere = Maps.newHashMap();
        superBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        superBudgetWhere.put("months", ParserUtil.getMonthOfPrevMonth());
        BigDecimal superMonthBudget = proBudgetOfgroupDtlService.sumMonthProfitOfSuper(superBudgetWhere);
        if (superMonthBudget == null || superMonthBudget.compareTo(BigDecimal.ZERO) == 0) {
            throw new ServiceException("[总部]经营质量分析指标-管理净利润 数据归集时，公司预算不存在~");
        }
        // 年度预算
        Map<String, Object> superYearBudgetWhere = Maps.newHashMap();
        superYearBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        BigDecimal superYearBudget = proBudgetOfgroupService.sumYearProfitOfSuper(superYearBudgetWhere);
        if (superYearBudget == null || superYearBudget.compareTo(BigDecimal.ZERO) == 0) {
            throw new ServiceException("[总部]经营质量分析指标-管理净利润 数据归集时，公司预算不存在~");
        }

        OrgSuperQa superQaAdd = new OrgSuperQa();
        superQaAdd.setHrOrgSuperId(BizParaUtil.get("super_org_id"));// 总部公司ID
        superQaAdd.setAimType(4);// 指标类型 4=管理净利润
        superQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
        superQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
        superQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月
        // 月度计划
        superQaAdd.setCurMonthPlan(superMonthBudget);// 原始单位：元
        superQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(superMonthBudget));
        superQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(superMonthBudget));
        // 年度计划
        superQaAdd.setYearAllPlan(superYearBudget);// 原始单位：元
        superQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(superYearBudget));
        superQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(superYearBudget));

        // 当月实际完成
        // 所有PPP项目 = sumSuperProfit 
        // 总部的管理净利润 = 所有PPP项目 + 南充管理净利润 + 建投总部管理口径管理净利润

        // 南充管理净利润 -当月实际完成
        LandProfit landProfitWhere = new LandProfit();
        landProfitWhere.setBeeDateMonth(superQaAdd.getStsMonth());
        landProfitWhere.setState(1);
        List<LandProfit> landProfitList = landProfitService.selectByEntityWhere(landProfitWhere);
        sumSuperProfit = sumSuperProfit.add(sumOfLandProfit(landProfitList));

        // 建投总部管理净利润 -当月实际完成
        SuperProfit superProfitWhere = new SuperProfit();
        superProfitWhere.setBeeDateMonth(superQaAdd.getStsMonth());
        superProfitWhere.setState(1);
        List<SuperProfit> superProfitList = superProfitService.selectByEntityWhere(superProfitWhere);
        sumSuperProfit = sumSuperProfit.add(sumOfSuperProfit(superProfitList));

        // 实际当月完成 
        superQaAdd.setCurMonthFinish(sumSuperProfit);
        superQaAdd.setCurMonthFinishTts(ParserUtil.unit2Tts(sumSuperProfit));
        superQaAdd.setCurMonthFinishHms(ParserUtil.unit2Hms(sumSuperProfit));
        if (superQaAdd.getCurMonthPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAdd.setCurMonthRate(BigDecimal.ZERO);
        }
        else {
            superQaAdd.setCurMonthRate(superQaAdd.getCurMonthFinish().divide(
                superQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));// 完成率 =实际完成/计划
        }

        Map<String, Object> prevWhere = Maps.newHashMap();
        prevWhere.put("hrOrgSuperId", BizParaUtil.get("super_org_id"));// 总部公司ID
        prevWhere.put("aimType", 4);// 指标类型 4=管理净利润
        prevWhere.put("belongsYear", superQaAdd.getBelongsYear());
        prevWhere.put("belongsMonth", superQaAdd.getBelongsMonth() - 1);// 前一个月
        prevWhere.put("orderCondition", " add_time desc ");
        List<OrgSuperQa> prevList = orgSuperQaService.selectByMapWhere(prevWhere);

        // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据 
        if (superQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan());
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts());
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms());
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish());
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts());
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms());
            superQaAdd.setYearAddupRate(superQaAdd.getCurMonthRate());
        }
        else {
            OrgSuperQa prevAreaQa = prevList.get(0);
            // 累加   当前月累 = 上月月累 + 当前
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan().add(
                prevAreaQa.getYearAddupPlan()));
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts().add(
                prevAreaQa.getYearAddupPlanTts()));
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms().add(
                prevAreaQa.getYearAddupPlanHms()));
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish().add(
                prevAreaQa.getYearAddupFinish()));
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts().add(
                prevAreaQa.getYearAddupFinishTts()));
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms().add(
                prevAreaQa.getYearAddupFinishHms()));
            // 计算月累完成率
            if (superQaAdd.getYearAddupPlan().compareTo(BigDecimal.ZERO) == 0) {
                superQaAdd.setYearAddupRate(BigDecimal.ZERO);
            }
            else {
                superQaAdd.setYearAddupRate(superQaAdd.getYearAddupFinish().divide(
                    superQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }
        }
        // 年度完成率  月累完成/年度计划 
        if (superQaAdd.getYearAllPlan().compareTo(BigDecimal.ZERO) == 0) {
            superQaAdd.setYearAllRate(BigDecimal.ZERO);
        }
        else {
            superQaAdd.setYearAllRate(superQaAdd.getYearAddupFinish().divide(
                superQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
        }
        superQaAdd.setAddBy("system");
        superQaAdd.setAddTime(new Date());
        superQaAdd.setUptBy("system");
        superQaAdd.setUptTime(new Date());
        orgSuperQaService.insert(superQaAdd);
    }

    /** 累加，为null时处理为zero **/
    private BigDecimal addUp(BigDecimal b1, BigDecimal b2) {
        if (b1 == null) {
            b1 = BigDecimal.ZERO;
        }
        if (b2 == null) {
            b2 = BigDecimal.ZERO;
        }
        return b1.add(b2);
    }

    /**
     * 管理净利润 =（建安毛利+创效金额+融资息差—管理费用—财务费用—税金（费用化））*0.75
     * @param grossProfit  建安毛利
     * @param effectFee  创效金额
     * @param finSpread  融资息差
     * @param manageFee  管理费用
     * @param fiaFee 财务费用
     * @param taxFee 税金
     * @return 
     * @see
     */
    private BigDecimal cpuProfit(BigDecimal grossProfit, BigDecimal effectFee,
                                 BigDecimal finSpread, BigDecimal manageFee, BigDecimal fiaFee,
                                 BigDecimal taxFee) {
        if (grossProfit == null) {
            grossProfit = BigDecimal.ZERO;
        }
        if (effectFee == null) {
            effectFee = BigDecimal.ZERO;
        }
        if (finSpread == null) {
            finSpread = BigDecimal.ZERO;
        }
        if (manageFee == null) {
            manageFee = BigDecimal.ZERO;
        }
        if (fiaFee == null) {
            fiaFee = BigDecimal.ZERO;
        }
        if (taxFee == null) {
            taxFee = BigDecimal.ZERO;
        }
        // （建安毛利+创效金额+融资息差—管理费用—财务费用—税金（费用化））*0.75
        BigDecimal temp = grossProfit.add(effectFee).add(finSpread).subtract(manageFee).subtract(
            fiaFee).subtract(taxFee);
        return temp.multiply(new BigDecimal("0.75")).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 合计费用
     */
    private BigDecimal sumOfSuperProfit(List<SuperProfit> list) {
        BigDecimal unit = BigDecimal.ZERO;
        for (SuperProfit var : list) {
            unit = unit.add(var.getManageProfit());// 管理利润
        }
        return unit;
    }

    /**
     * 合计费用
     */
    private BigDecimal sumOfLandProfit(List<LandProfit> list) {
        BigDecimal unit = BigDecimal.ZERO;
        for (LandProfit var : list) {
            unit = unit.add(var.getNetProfit());// 净利润
        }
        return unit;
    }
}
